package com.zzh.structuredeployment.activity

import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.zzh.bridge.page.PageApp
import com.zzh.bridge_annotation.RouteTag
import com.zzh.structuredeployment.R
import com.zzh.tools.listener.ToolOnClickListener
import com.zzh.tools.system.ToolScreen
import com.zzh.tools.utils.ToolLog
import com.zzh.tools.utils.ToolMap
import kotlinx.android.synthetic.main.activity_nine.*

@RouteTag(value = PageApp.MENU)
class NineActivity : AppCompatActivity() {

    private var isShow = true

    private var dip2px5: Int = 0
    private var dip2px10: Int = 0
    private var dip2px60: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nine)

        dip2px5 = ToolScreen.dip2px(this, 5f)
        dip2px10 = ToolScreen.dip2px(this, 10f)
        dip2px60 = ToolScreen.dip2px(this, 60f)


        var list = createMenus()

        var toolOnClickListener = object : ToolOnClickListener() {
            override fun onClick(v: View?) {
                super.onClick(v)
                if (id == -1) {
                    return
                }
                ToolLog.i("zzh", ">position>>${v?.tag}>>>>")
            }
        }

        llReuseAddView.setOnAssignmentOperation { _, rootView, position ->
            var iconImage = rootView.findViewById<ImageView>(R.id.icon)
            var menuText = rootView.findViewById<TextView>(R.id.menuText)
            var map = list[position]
            menuText.text = map["name"] as String
            iconImage.setImageResource(map["icon"] as Int)
            menuText.setPadding(
                0,
                dip2px5,
                0,
                if (position == list.size - 1) dip2px60 else dip2px10
            )
            rootView.tag = position
            rootView.setOnClickListener(toolOnClickListener)
        }
        llReuseAddView.configCount(list.size)

        llReuseAddView.viewTreeObserver.addOnPreDrawListener(object :
            ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                llReuseAddView.viewTreeObserver.removeOnPreDrawListener(this)
                var count = llReuseAddView.childCount
                isShow = true
                for (i in 0 until count) {
                    var view = llReuseAddView.getChildAt(i)
                    createShowAnimation(view, llReuseAddView.height - view.top.toFloat())
                }
                operation.text = "隐藏"
                return true
            }
        })

        operation.setOnClickListener {

            var count = llReuseAddView.childCount
            if (isShow) {
                isShow = false
                for (i in 0 until count) {
                    var view = llReuseAddView.getChildAt(i)
                    createHiddenAnimation(view, llReuseAddView.height - view.top.toFloat())
                }
                operation.text = "显示"
            } else {
                isShow = true
                for (i in 0 until count) {
                    var view = llReuseAddView.getChildAt(i)
                    createShowAnimation(view, llReuseAddView.height - view.top.toFloat())
                }
                operation.text = "隐藏"
            }

        }
    }

    fun createMenus(): ArrayList<Map<String, Any>> {
        var list = arrayListOf<Map<String, Any>>()
        list.add(ToolMap.createMap("name", "21", "icon", android.R.drawable.ic_menu_delete))
        list.add(ToolMap.createMap("name", "12", "icon", android.R.drawable.ic_menu_zoom))
        list.add(ToolMap.createMap("name", "下载", "icon", android.R.drawable.ic_menu_edit))
        list.add(ToolMap.createMap("name", "分享", "icon", android.R.drawable.ic_menu_share))
        return list
    }

    fun createHiddenAnimation(childView: View, toYValue: Float) {
        var animation = TranslateAnimation(
            Animation.ABSOLUTE,
            0f,
            Animation.ABSOLUTE,
            0f,
            Animation.ABSOLUTE,
            0f,
            Animation.ABSOLUTE,
            toYValue
        )
        animation.duration = 500
        animation.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                childView.visibility = View.INVISIBLE

            }

            override fun onAnimationStart(animation: Animation?) {
                childView.isClickable = false
            }

        })
        childView.startAnimation(animation)
    }

    fun createShowAnimation(childView: View, fromYValue: Float) {
        var animation = TranslateAnimation(
            Animation.ABSOLUTE,
            0f,
            Animation.ABSOLUTE,
            0f,
            Animation.ABSOLUTE,
            fromYValue,
            Animation.ABSOLUTE,
            0f
        )
        animation.duration = 500
        animation.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                childView.isClickable = true
            }

            override fun onAnimationStart(animation: Animation?) {
                childView.visibility = View.VISIBLE
            }

        })
        childView.startAnimation(animation)
    }


}